package com.newenergy.wisehome.data.ormlite.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.newenergy.wisehome.common.helper.LogHelper;
import com.newenergy.wisehome.data.bean.Fan;
import com.newenergy.wisehome.data.ormlite.db.DatabaseHelper;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 风扇数据库操作
 */

public class FanDao {
    private Context mContext;
    private Dao<Fan, Integer> fanDaoOpe;
    private DatabaseHelper helper;

    public FanDao(Context context) {
        mContext = context;
        try {
            helper = DatabaseHelper.getHelper(mContext);
            fanDaoOpe = helper.getDao(Fan.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 增加一个用户
     *
     * @param
     * @throws SQLException
     */
    public void add(Fan fan) {
        try {
            fan.setChecked(false);
            fanDaoOpe.create(fan);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    public int updateFan(Fan fan) {
        try {
            fan.setChecked(false);
            return fanDaoOpe.update(fan);
            //    LogHelper.i("updateLightFannumber" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public Fan get(int id) {
        try {
            return fanDaoOpe.queryForId(id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public Fan getByUuid(String uuid) {
        List<Fan> fanList = null;
        try {
            fanList = fanDaoOpe.queryBuilder().where().eq ("uuid", uuid).query();
            if (fanList != null && fanList.size() > 0) {
                return fanList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    

    /**
     * @return
     */
    public List<Fan> getAllFan() {
        List<Fan> fanList = new ArrayList<>();
        try {
            fanList = fanDaoOpe.queryBuilder().query();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fanList;
    }

    public Fan getById(String groupId,int category) {
        List<Fan> fanList = null;
        try {
            fanList = fanDaoOpe.queryBuilder().where().eq
                    ("groupId", groupId).and().eq
                    ("category", category).query();
            if (fanList != null && fanList.size() > 0) {
                return fanList.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }


    public List<Fan> getFamilyFans(String familyId,int category) {
        List<Fan> fanList = new ArrayList<>();
        try {
            fanList = fanDaoOpe.queryBuilder().where().eq ("familyId", familyId).and().eq
                    ("category", category).query();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return fanList;
    }

    public int delete(Fan fan) {
        try {
            return fanDaoOpe.delete(fan);
            //    LogHelper.i("deleLightFannumber==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public void deleteAll() {
        try {
            int number = fanDaoOpe.deleteBuilder().delete();
            // LogHelper.i("deleteAll number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void delete(List<Fan> fanList) {
        try {
            int number = fanDaoOpe.delete(fanList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    public void deleteFamilyFans(String familyId) {
        try {
            List<Fan> fanList = getFamilyFans(familyId,2);
            int number = fanDaoOpe.delete(fanList);
            LogHelper.i("delete number==" + number);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public Fan getFanByName(String familyId, String name,int category) {
        try {
            List<Fan> fanList = fanDaoOpe.queryBuilder().where().eq
                    ("familyId", familyId).and().eq
                    ("name", name).and().eq
                    ("category", category).query();
            if (fanList != null && fanList.size() > 0)
                return fanList.get(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
